﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLK.Database;
using QLK.Database.BLLs;
using QLK.Web.App_Code;

namespace QLK.Web
{
    public partial class ProductsDetails : System.Web.UI.Page
    {
        ProductBLL productsBLL = new ProductBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                Load_Data();
        }

        private void Load_Data()
        {
            grvProducts.DataSource = productsBLL.ProductGetForGrid();
            grvProducts.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect(PageUrls.Products);
        }

        protected void grvProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvProducts.PageIndex = e.NewPageIndex;
            Load_Data();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            CheckBox ck;

            for (int i = 0; i < grvProducts.Rows.Count; i++)
            {
                ck = (CheckBox)grvProducts.Rows[i].Cells[1].FindControl("ckDelete");

                if (ck.Checked == true)
                {
                    int productid = int.Parse(ck.Attributes["_ProID"]);

                    if (productsBLL.ProductDeleteByID(productid))
                    {
                        lblWarning.Text = "Success";
                    }
                    else
                        lblWarning.Text = "fails";
                }
            }
            Load_Data();
        }

        protected void ImgEdit_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton img = sender as ImageButton;

            Response.Redirect(string.Format("{0}?{1}={2}",
               PageUrls.Products, Columns.ProductID, img.CommandArgument));
        }

        protected void grvProducts_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
